<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class Berita extends CI_Controller
{
    public function index()
    {
        $data['rslt_data_berita'] = $this->db->query('SELECT * FROM berita');
        $this->template->admin('admin/v_berita', $data, 4, "Berita Akademik");
    }
	
	public function tambah()
	{
		$this->load->library('form_validation');
		$this->_rule_validation();
		if ($this->form_validation->run() === FALSE)
		{
			$data['id'] = '';
			$data['judul_berita'] = '';
			$data['isi_berita'] = '';
			$this->template->admin('admin/v_berita_form', $data, 4, "Form Tambah Berita Akademik");
		}
		else
		{
			$this->_simpan('TAMBAH');
		}	
	}
    
    public function edit($id = null)
    {
        if($id === null)
        {
            show_404();
        }
        else
        {
            $rslt_data = $this->db->select('*')->from('berita')->where('id', $id)->limit(1)->get();
            if($rslt_data->num_rows() == 1)
            {
				$this->load->library('form_validation');
				$this->_rule_validation();
				if ($this->form_validation->run() === FALSE)
				{
					$rslt_data = $rslt_data->row();
					$data['id'] = $rslt_data->id;
					$data['judul_berita'] = $rslt_data->judul;
					$data['isi_berita'] = $rslt_data->berita_isi;
					$this->template->admin('admin/v_berita_form', $data, 4, "Form Ubah Berita Akademik");
				}
				else
				{
					$this->_simpan('UBAH');
				}
            }
            else
            {
                show_404();
            }
        }
    }
    
    public function hapus($id = null)
    {
        if($id === null)
        {
            show_404();
        }
        else
        {
            $this->db->where('id', $id)->limit(1)->delete('berita');
            redirect('admin/berita');
        }
    }
    
    public function _simpan($proses = null)
    {
        if($proses == null)
        {
            show_404();
        }
        else
        {
            $judul_berita = $this->input->post('judul_berita');
            $isi_berita = $this->input->post('isi_berita');
			$url_image = null;
			if($_FILES['foto']['name'] != "")
			{
				$nama_file = $_FILES['foto']['name'];
				$gambar = md5(uniqid($nama_file, true)) . "." . end(explode(".", $nama_file));

				//Save Image
				$type = $_FILES['foto']['type'];
				if (strstr($type, 'image') != ''):

					$path = "data/gambar/home/";
					if (!file_exists($path)) {
						mkdir($path);
					}
					move_uploaded_file($_FILES['foto']['tmp_name'], $path . $nama_file);
					rename($path . $nama_file, $path . $gambar);
					
				endif;
				$url_image = "home/" . $gambar;
			}
            if($proses == 'TAMBAH')
            {
				$this->db->insert('berita', 
								array('judul' => $judul_berita,			
								'berita_isi' => $isi_berita,
								'gambar' => $url_image)
								);
            }
            else if($proses == 'UBAH')
            {
                $id = $this->input->post('id');
                if($id != '')
                {
					if($url_image != null)
                    $this->db->set( 
							array('judul' => $judul_berita,			
							'berita_isi' => $isi_berita,
							'gambar' => $url_image)
                            );
					else
                    $this->db->set( 
							array('judul' => $judul_berita,			
							'berita_isi' => $isi_berita)
                            );
							
                    $this->db->where('id', $id)
                        ->update('berita');
                }
            }
            redirect('admin/berita');
        }
    }
	
	private function _rule_validation()
	{
		$this->form_validation->set_message('required', '%s harap di isi');
		$this->form_validation->set_rules('judul_berita', 'Judul Berita', 'required|max_length[32]|trim');
		$this->form_validation->set_rules('isi_berita', 'Isi Berita', 'required|trim');
		$this->form_validation->set_rules('foto', 'Gambar Berita', '');
	}
}

/* End of file berita.php */
/* Location: ./application/controllers/admin/berita.php */